import 'dart:math';

import 'package:flutter/material.dart';

void main() {
  runApp(new MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: Text("Listener触摸事件"),
      ),
      body: HomeScreen(),
    ),
  ));
}

class HomeScreen extends StatefulWidget {
  @override
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Listener(
          behavior: HitTestBehavior.deferToChild,
          //default
          onPointerDown: (PointerDownEvent event) {
            //event.pressure
            //event.delta 两次移动间距
            //event.pointer
            print("point down ${event.position}");
          },
          onPointerMove: (event) {
            print("point move ${event.position}");
          },
          onPointerCancel: (event) {
            print("point cancel ${event.position}");
          },
          onPointerEnter: (event) {
            print("point enter ${event.position}");
          },
          onPointerExit: (event) {
            print("point exit ${event.position}");
          },
          onPointerHover: (event) {
            print("point hover ${event.position}");
          },
          onPointerSignal: (event) {
            print("point signal ${event.position}");
          },
          onPointerUp: (event) {
            print("point up ${event.position}");
          },
          child: Center(
            child: Container(
              width: 300,
              height: 100,
              color: Colors.cyan,
              child: Center(
                child: Text("click me!"),
              ),
            ),
          ),
        ),
        Listener(
          behavior: HitTestBehavior.opaque,
          onPointerDown: (PointerDownEvent event) {
            print("point down ${event.position} 1");
          },
          child: ConstrainedBox(
            constraints: BoxConstraints.tight(Size(300, 100)),
            child: Center(
              child: Listener(
                  onPointerDown: (event){
                    print("down text 1");
                  },
                  child: Text("click me!")),
            ),
          ),
        ),
        Listener(
          behavior: HitTestBehavior.opaque,
          onPointerDown: (PointerDownEvent event) {
            print("point down ${event.position} 2");
          },
          child: ConstrainedBox(
            constraints: BoxConstraints.tight(Size(300, 100)),
            child: Listener(
              //absorb本身处理事件
              onPointerDown: (event)=>print("absorb"),
              child: AbsorbPointer(
                child: Listener(
                    onPointerDown: (event){
                      print("down text2");
                    },
                    child: Text("click me!")),
              ),
            ),
          ),
        ),
        Listener(
          behavior: HitTestBehavior.opaque,
          onPointerDown: (PointerDownEvent event) {
            print("point down ${event.position} 3");
          },
          child: ConstrainedBox(
            constraints: BoxConstraints.tight(Size(300, 100)),
            child: Listener(
              // ignore本身不会接收处理事件
              onPointerDown: (event)=>print("ignore ?"),
              child: IgnorePointer(
                child: Listener(
                    onPointerDown: (event){
                      print("down text3");
                    },
                    child: Text("click me!")),
              ),
            ),
          ),
        )
      ],
    );
  }
}
